// pages/health/searchResult/searchResult.js
Page({
  data: {
    keyword: '',
    resultList: []
  },

  onLoad(options) {
    if (options && options.q) {
      this.setData({ keyword: decodeURIComponent(options.q) });
    }
    this.mockFetch();
  },

  goBack() { wx.navigateBack(); },
  onInput(e) { this.setData({ keyword: e.detail.value }); },
  clearKeyword() { this.setData({ keyword: '' }); },
  onConfirm() { this.onSearch(); },
  onSearch() {
    const q = (this.data.keyword || '').trim();
    if (!q) { wx.showToast({ title: '请输入关键字', icon: 'none' }); return; }
    this.mockFetch(q);
  },

  // 模拟请求，生成若干条数据
  mockFetch(q) {
    const base = ['苹果', '香蕉苹果', '阿克苏苹果', '青香蕉苹果', '苹果(去皮,烤)', '苹果可颂'];
    const names = base.map(n => (q ? `${q}` : '苹果') + n.replace('苹果',''));
    const list = names.map((name, i) => ({
      id: i + 1,
      name,
      calorie: [50, 52, 53, 86, 253][i % 5],
      image: '/static/upload/meishi1.jpg',
      level: ['green','green','green','yellow','red'][i % 5]
    }));
    this.setData({ resultList: list });
  }
})